package com.zzx.装饰器模式;

/**
 * 某发票（lnvoice）由抬头（Head）部分、正文部分和脚注（Foot）部分构成。现采用装饰（Decorator）模式实现打印发票的功能，得到如图6-1所示的类图。
 * 程序的输出结果为：
 * This is the header of the invoice!
 * This is the content of the invoice!
 * This is the footnote of the invoice!
 * --------------------
 * This is the header of the invoice!
 * This is the footnote of the invoice!
 */
class Invoice {
    public void printInvoice() {
        System.out.println("This is the content of the invoice!");
    }
}


class Decorator extends Invoice {
    protected Invoice ticket;

    public Decorator(Invoice t) {
        ticket = t;
    }

    public void printInvoice() {
        if (ticket != null)
            ticket.printInvoice();
    }
}

class HeadDecorator extends Decorator {
    public HeadDecorator(Invoice t) {
        super(t);
    }

    public void printInvoice() {
        System.out.println("This is the header of the invoice!");
        super.printInvoice();
    }
}

class FootDecorator extends Decorator {
    public FootDecorator(Invoice t) {
        super(t);
    }

    public void printInvoice() {
        super.printInvoice();
        System.out.println("This is the footnote of the invoice!");
    }
}

class Test {
    public static void main(String[] args) {
        Invoice t = new Invoice();
        Invoice ticket;
        ticket = new HeadDecorator(new FootDecorator(t));
        ticket.printInvoice();
        System.out.println("--------------------");
        ticket = new HeadDecorator(new FootDecorator(null));
        ticket.printInvoice();
    }
}
